package com.controller;

import com.bean.Clazz;
import com.bean.ClazzWithStudent;
import com.bean.Student;
import com.service.ClazzService;
import com.service.StudentService;
import com.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 末日流光
 * @date 2022/5/12 14:50
 * @project SSM-demo1
 * @company dzy
 */
@Controller
@RequestMapping(value = "/admin")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    private StudentService studentService;

    /**
     *
     * 班级信息页面
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:05
     */

    @RequestMapping(value ="/clazz_list_admin")
    public String clazz_list_admin(){
        return "clazz/clazz_list_admin";
    }
    /**
     *
     * 添加班级
     * @param clazz
     * @param model
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:06
     */

    @RequestMapping(value = "/addClazz")
    public String addClazz(Clazz clazz, Model model){
        clazzService.addClazz(clazz);
        model.addAttribute("Clazzs", clazzService.getClazzs());
        return "index";
    }
    /**
     *
     * 分页查询班级
     * @param page
     * @param limit
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:05
     */

    @RequestMapping(value = "/AdminClazzListServlet")
    @ResponseBody
    public ResultVo AdminClazzListServlet(int page, int limit){
        ResultVo resultVo = new ResultVo();
        List<Clazz> clazzs=clazzService.getClazzsByPage(page,limit);
        int length = clazzService.getClazzsLength();
        resultVo.setData(clazzs);
        resultVo.setCount(length);
        resultVo.setMsg("success");
        return resultVo;
    }
    /**
     *
     * 添加班级信息
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:04
     */

    @RequestMapping(value ="/AdminAddClazzServlet1")
    public String AdminAddClazzServlet1(){
        return "clazz/add_clazz";
    }
    @RequestMapping(value ="/AdminAddClazzServlet2")
    @ResponseBody
    public ResultVo AdminAddClazzServlet2(Clazz clazz){
        ResultVo resultVo=new ResultVo();
        int i=clazzService.addClazz(clazz);
        if(i==1){
            resultVo.setMsg("添加成功");
        }else{
            resultVo.setMsg("添加失败");
        }
        return resultVo;
    }
    /**
     *
     * 跳转到编辑页面
     * @param id
     * @param model
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:04
     */

    @RequestMapping(value ="/AdmineditClazzServlet")
    public String AdmineditClazzServlet(@RequestParam("id") int id,Model model){
       Clazz clazz=clazzService.selectClazzById(id);
       model.addAttribute("clazz",clazz);
       return "clazz/edit_clazz";
    }
    /**
     *
     * 编辑班级信息
     * @param clazz
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:04
     */

    @RequestMapping(value ="/AdminEditClazzServlet2")
    @ResponseBody
    public ResultVo AdminEditClazzServlet2(Clazz clazz){
        ResultVo resultVo=new ResultVo();
        int i=clazzService.updateClazzById(clazz);
        if(i==1){
            resultVo.setMsg("编辑成功");
        }else{
            resultVo.setMsg("编辑失败");
        }
        return resultVo;
    }
    /**
     *
     * 根据班级编号删除班级
     * @param id
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:04
     */

    @RequestMapping(value ="/delClazzById")
    @ResponseBody
    public ResultVo delClazzById(@RequestParam("id")int id){
        ResultVo resultVo=new ResultVo();
        List<Student> students=studentService.selectStudentBycId(id);
        if (students.size()!=0){
            resultVo.setMsg("删除失败,还有学生");
        }else {
            int i = clazzService.delClazzById(id);
            if (i == 1) {
                resultVo.setMsg("删除成功");
            } else {
                resultVo.setMsg("删除失败");
            }
        }
        return resultVo;
    }
    /**
     *
     * 根据班级编号批量删除班级
     * @param cIds
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:03
     */

    @RequestMapping(value ="/deleteClazzBatch")
    @ResponseBody
    public ResultVo deleteBatch(@RequestParam("cIds") String[] cIds) {
        ResultVo resultVo = new ResultVo();
        int flag=0;
        for(String id:cIds){
            List<Student> students=studentService.selectStudentBycId(Integer.parseInt(id));
            if (students.size()!=0){
                flag=1;
            }else {
                clazzService.delClazzById(Integer.parseInt(id));
            }
        }
        if(flag==1) {
            resultVo.setMsg("部分班级删除失败,因为班级里还有学生");
        }
        else {
            resultVo.setData(cIds);
            resultVo.setMsg("删除成功");
        }
        return resultVo;
    }
    /**
     *
     * 根据班级名模糊查询学生
     * @param clazzName
     * @param page
     * @param limit
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:02
     */

    @RequestMapping(value = "/selectStudentByClazzName")
    @ResponseBody
    public ResultVo selectStudentByClazzName(@RequestParam("clazzName") String clazzName,int page,int limit){
//        System.out.println(clazzName);
        List<ClazzWithStudent> studentsWithClazz=clazzService.selectStudentByClazzName(clazzName);
        List<Student> students=new ArrayList<>();
        for (ClazzWithStudent sc: studentsWithClazz) {
           students.addAll(sc.getStudents());
        }
        int start=(page-1)*limit;
        ResultVo resultVo = new ResultVo();
        int length=students.size();
        int sizeList=length-1;
        if (start+limit-1>sizeList){
            sizeList=length-1;
        }else
        {
            sizeList=start+limit-1;
        }
        resultVo.setData(students.subList(start,sizeList));
        resultVo.setCount(length);
        resultVo.setMsg("查询成功");
        return resultVo;
    }

}

